package com.run.bcpimp.extract;

import java.io.IOException;
import java.util.Calendar;
import java.util.Map;

import org.jdom2.Element;

import com.run.bcpimp.conf.DbFieldConfig;
import com.run.bcpimp.exception.BcpConfigurationException;
import com.run.bcpimp.processor.BcpRecordContext;

/**
 * 
 * @author chenxu
 *
 */
public class DateSetter extends FieldsSetter {
	
	public String dbFieldName;
	/**
	 * 使用 {@link Calendar}中的各种标示来标示，日周月，详见
	 * 下面的 {@link #setFields(BcpRecordContext)}方法的实现
	 */
	public int calendarFlag;
	public int dbIndex;
	public boolean isNull;
	public int length;
	private _DateSetter dateSetter;
	private Map<String, DbFieldConfig> dbFieldsMap;

	@Override
	public boolean init(Element setterEle) throws BcpConfigurationException {
		dbFieldsMap = cfg.getDbTableConfig().getDbFieldsMap();
		
		if(null!=setterEle) {
			String mode = setterEle.getAttributeValue("mode");
			if("formated".equalsIgnoreCase(mode)) {
				dateSetter = new ToFormatedString();
			} else {
				dateSetter = new ToTimestamp();
			}
			log.info("_datesetter "+dateSetter);

			dateSetter.init(setterEle);
					
		} else {
			return false;
		}
		
		return true;
	}

	@Override
	public boolean setFields(BcpRecordContext bcp) throws IOException,
			InterruptedException {
			
		dateSetter.setFields(bcp);

		return false;
	}
	
	private  interface _DateSetter {
		public boolean setFields(BcpRecordContext bcp) throws IOException,
		InterruptedException ;
	public boolean init(Element setterEle) throws BcpConfigurationException; 
	}
	
	/**
	 * 输出格式化后的日期字符串
	 * @author user
	 *
	 */
	private  class ToFormatedString implements _DateSetter {

		@Override
		public boolean setFields(BcpRecordContext bcp) throws IOException,
				InterruptedException {
			
			switch (calendarFlag) {
			case Calendar.DAY_OF_MONTH://日
				colValues[dbIndex] = String.valueOf(bcp.getCalendar().get(Calendar.YEAR)*10000+(1+bcp.getCalendar().get(Calendar.MONTH))*100+bcp.getCalendar().get(Calendar.DAY_OF_MONTH));
				break;
			case Calendar.WEEK_OF_YEAR://周
				colValues[dbIndex] = String.valueOf(bcp.getCalendar().get(Calendar.YEAR)*100+bcp.getCalendar().get(Calendar.WEEK_OF_YEAR));
				break;
			case Calendar.MONTH://月
				colValues[dbIndex] = String.valueOf(bcp.getCalendar().get(Calendar.YEAR)*100+(1+bcp.getCalendar().get(Calendar.MONTH)));
				break;
			default:
				break;
			}

			return false;
		}

		@Override
		public boolean init(Element setterEle) throws BcpConfigurationException {
			Element itemEle = setterEle.getChild("item");
			String dbFieldNameVlue = itemEle.getAttributeValue("dbFieldName");
			DbFieldConfig dbFieldConfig = dbFieldsMap.get(dbFieldNameVlue);
			if(null==dbFieldConfig) {
				throw new BcpConfigurationException("item configed dbFieldName["+dbFieldNameVlue+"] not exists, bcp["+bc.getName()+"] extractor["+cfg.getId()+"] map:"+dbFieldsMap);
			}
			
			calendarFlag = Integer.parseInt(itemEle.getAttributeValue("calendarFlag"));
			dbIndex = dbFieldConfig.colIndex;
			return true;
		}
		
	}
	
	/**
	 * 输出原始的long值代表的日期
	 * @author user
	 *
	 */
	private  class ToTimestamp implements _DateSetter {

		@Override
		public boolean setFields(BcpRecordContext bcp) throws IOException,
				InterruptedException {
			colValues[dbIndex] =  String.valueOf(bcp.getTimestamp()); 
			
			return false;
		}

		@Override
		public boolean init(Element setterEle) throws BcpConfigurationException {
			Element itemEle = setterEle.getChild("item");
			String dbFieldNameVlue = itemEle.getAttributeValue("dbFieldName");
			DbFieldConfig dbFieldConfig = dbFieldsMap.get(dbFieldNameVlue);
			if(null==dbFieldConfig) {
				throw new BcpConfigurationException("item configed dbFieldName["+dbFieldNameVlue+"] not exists, bcp["+bc.getName()+"] extractor["+cfg.getId()+"] map:"+dbFieldsMap);
			}
			dbIndex = dbFieldConfig.colIndex;
			return true;
		}
		
	}

}
